<template>
    <div>

        <!-- 表单 -->
        <!-- 弹出层表单————修改 & 新增 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="65%" center append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item hidden label="药品ID" prop="medicinesId" size="mini">
                    <el-input v-model="form.medicinesId"/>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="药品名称" prop="medicinesName">
                            <el-input v-model="form.medicinesName" clearable placeholder="请输入药品名称"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="药品编号" prop="medicinesNumber">
                            <el-input v-model="form.medicinesNumber" clearable placeholder="请输入药品编码"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="关键字" prop="keywords">
                            <el-input v-model="form.keywords" clearable  placeholder="请输入关键字"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单 位" prop="unit">
                            <el-input v-model="form.unit" clearable placeholder="请输入单位"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="换算量" prop="conversion">
                            <el-input v-model="form.conversion" clearable placeholder="请输入换算量"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="处方价格" prop="prescriptionPrice">
                            <el-input v-model="form.prescriptionPrice" clearable placeholder="请输入处方价格"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="预警值" prop="medicinesStockDangerNum">
                            <el-input-number v-model="form.medicinesStockDangerNum" style="width:340px" placeholder="请输入预警值"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-radio v-model="form.status" label="0">正常</el-radio>
                            <el-radio v-model="form.status" label="1">停用</el-radio>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="药品类型" prop="medicinesType">
                            <el-select v-model="form.medicinesType" clearable  style="width:340px" placeholder="请输入药品类型">
                                <el-option v-for="dict in statusOptions1"
                                           :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="处方类型" prop="prescriptionType">
                            <el-select v-model="form.prescriptionType" clearable style="width:340px" placeholder="请输入处方类型">
                                <el-option v-for="dict in this.statusOptions2"
                                           :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="生产厂家" prop="producterId">
                            <el-select v-model="form.producterId" clearable placeholder="请输入生产厂家" style="width:340px">
                                <el-option v-for="item in deptData"
                                           :key="item.producerId" :label="item.producerName" :value="item.producerId"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item style="text-align: center;">
                    <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
                    <el-button @click="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

<!--调整库存-->
        <el-dialog title="调整库存" :visible.sync="dialogVisible1" width="30%">
			<span>
				<el-form ref="form" :model="form" label-width="80px">
					<el-form-item label="请输入要调整的值">
						<el-input v-model="form.medicinesStockNum"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit1('form')">立即创建</el-button>
						<el-button @click="aaa()">取消</el-button>
					</el-form-item>
				</el-form>
			</span>
        </el-dialog>
        <div>
            药品名称<el-input v-model="searchForm.medicinesName" placeholder="请输入药品名称" style="width: 100px;"></el-input>
            关键字<el-input v-model="searchForm.keywords" placeholder="请输入关键字" style="width: 100px;"></el-input>
            状态<el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option
                    v-for="item in statusOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
            </el-option>
        </el-select>
            药品类型<el-select v-model="searchForm.medicinesType" placeholder="药品类型">
            <el-option
                    v-for="item in statusOptions1"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
            </el-option>
        </el-select>
            处方类型<el-select v-model="searchForm.prescriptionType" placeholder="处方类型">
            <el-option
                    v-for="item in statusOptions2"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
            </el-option>
        </el-select>
            生产厂家<el-select v-model="searchForm.producterId" placeholder="生产厂家">
            <el-option v-for="item in deptData"
                       :key="item.producerId"
                       :label="item.producerName"
                       :value="item.producerId">
            </el-option>
        </el-select>
            <el-button type="primary" @click="queryAll()">搜索</el-button>
            <el-button type="primary" @click="resetFormFiled()">重置</el-button>
        </div>
        <div>
            <el-button type="primary" style="float: left;" @click="addUser()">添加</el-button>
            <el-button type="danger" @click="addUser1()" round>删除</el-button>
        </div>
        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="单位">
                            <span>{{ props.row.unit }}</span>
                        </el-form-item>
                        <el-form-item label="换算量">
                            <span>{{ props.row.conversion }}</span>
                        </el-form-item>
                        <el-form-item label="库存量">
                            <span>{{ props.row.medicinesStockNum }}</span>
                        </el-form-item>
                        <el-form-item label="预警值">
                            <span>{{ props.row.medicinesStockDangerNum }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column
                    label="药品 ID"
                    prop="medicinesId">
            </el-table-column>
            <el-table-column
                    label="药品名称"
                    prop="medicinesName">
            </el-table-column>
            <el-table-column
                    label="药品编号"
                    prop="medicinesNumber">
            </el-table-column>
            <el-table-column
                    label="生产厂家"
                    prop="producer.producerName">
            </el-table-column>
            <el-table-column
                    label="药品类型"
                    prop="medicinesType" :formatter="statusformat1">
            </el-table-column>
            <el-table-column
                    label="处方类型"
                    prop="prescriptionType" :formatter="statusformat2">
            </el-table-column>
            <el-table-column
                    label="关键字"
                    prop="keywords">
            </el-table-column>
            <el-table-column
                    label="处方价格"
                    prop="prescriptionPrice">
            </el-table-column>
            <el-table-column
                    label="状态"
                    prop="status" :formatter="statusformat">
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button @click="updUser(scope.row)" type="text" size="small" icon="el-icon-edit">修改</el-button>
                    <el-button @click="delUser(scope.row)" type="text" size="small" icon="el-icon-delete">删除</el-button>
                    <el-button @click="adjustment(scope.row)" type="text" size="small" icon="el-icon-delete">调整库存</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="page.seze"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Producter",
        methods:{

            //表单提交
            onSubmit(formName ){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("medicine/Medicines/addStockProducerMapper/",this.form).then(r=>{
                            this.dialogVisible=false;
                            this.initTable();
                            this.form={};
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //处方类型
            statusformat2(row){
                let val=row.prescriptionType ;
                return this.formatDict(this.statusOptions2,val)
            },
            //药品类型
            statusformat1(row){
                let val=row.medicinesType ;
                return this.formatDict(this.statusOptions1,val)
            },
            //状态字典解析
            statusformat(row){
                let val=row.status ;
                return this.formatDict(this.statusOptions,val)
            },
            //状态
            initStatus(){
                this.$axios.get("system/api/dict/data/findByType/sys_normal_disable").then(r=>{
                    this.statusOptions = r.data.t;
                })
            },
            //药品类型
            initStatus1(){
                this.$axios.get("system/api/dict/data/findByType/his_medicines_type").then(r=>{
                    this.statusOptions1 = r.data.t;
                })
            },
            //处方类型
            initStatus2(){
                this.$axios.get("system/api/dict/data/findByType/his_prescription_type").then(r=>{
                    this.statusOptions2 = r.data.t;
                })
            },
            //批量
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            //批量删除
            addUser1(){
                // let aaa = []
                // let bbb=[]
                // this.multipleSelection.forEach(item=>{
                //     aaa = {providerId:item.providerId}
                //     bbb.push(aaa)
                // })
                // console.log(JSON.stringify(bbb))
                // alert(JSON.stringify(this.multipleSelection))
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.post("medicine/Medicines/delByIds", this.multipleSelection).then(r => {
                        this.initTable();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //提交表单
            onSubmit1(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("medicine/Medicines/addStockProducerMapper",this.form).then(r=>{
                            this.dialogVisible1=false;
                            this.initTable();
                            alert("sas")
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
                // this.$axios.post('medicine/Medici/addStockProducerMapper',this.form).then(r=>{
                //     this.dialogVisible1=false;
                //     this.initTable();
                // })
 },
            adjustment(row){
                this.form={};
                this.form=JSON.parse(JSON.stringify(row));
                this.dialogVisible1=true;
            },
            queryAll(){
                this.page.current=1;
                this.initTable();
            },
            //生产厂家下拉框
            queryDept(){
                this.$axios.get('medicine/producer/getAll1').then(r=>{
                    this.deptData=r.data.t;
                    // alert(JSON.stringify(this.deptData))
                })
            },
            resetFormFiled(){
                this.searchForm={};
                this.initTable();
            },
            //分页
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.page.size=val;
                this.initTable();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.page.current=val;
                this.initTable();
            },
            //修改
            updUser(row){
                this.title="修改药品信息";
                this.dialogVisible=true;
                this.form=JSON.parse(JSON.stringify(row));
            },
            //取消提交表单

            aaa(){
                this.dialogVisible1=false;
                this.form={};
            },
            // 添加
            addUser() {
                this.title="添加药品信息";
                this.dialogVisible=true;
                this.form={status:'0'};
            },
            // 删除
            delUser(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete(`medicine/Medicines/delById/${row.medicinesId}`).then(r => {
                        this.initTable();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //刷新表格  //模糊查询

            initTable() {
                let search={...this.page,...this.searchForm};
                this.$axios.get('medicine/Medicines/getAll',{params:search}).then(r => {
                    this.tableData = r.data.t.records;
                    //刷新总条数
                    this.page.total=r.data.t.total;
                })
            }
        },
        data() {
            return {
                tableData: [],
                dialogVisible: false,
                form: {},
                title:'',
                queryParams:{},
                page:{
                    total:10,
                    current:1,
                    size:10
                },
                searchForm: {},
                deptData:[],
                dialogVisible1:false,
                multipleSelection: [],
                statusOptions:[],
                statusOptions1:[],
                statusOptions2:[],
                producterNameOption:[],
                title:'',
                dictList:[],
                rules: {
                    medicinesName: [
                        { required: true, message: '请输入药品名称3-10', trigger: 'blur' },
                        { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ],
                    medicinesNumber: [
                        { required: true, message: '药品编码不能为空', trigger: 'blur' }
                    ],
                    unit: [
                        { required: true, message: '单位不能为空', trigger: 'blur' }
                    ],
                    conversion: [
                        { required: true, message: '换算量不能为空', trigger: 'blur' }
                    ],
                    prescriptionPrice: [
                        { required: true, message: '处方价格不能为空', trigger: 'blur' }
                    ],
                    medicinesType: [
                        { required: true, message: '请选择药品类型', trigger: 'change' }
                    ],
                    prescriptionType: [
                        { required: true, message: '请选择处方类型', trigger: 'change' }
                    ],
                    producterId: [
                        { required: true, message: '请选择生产厂家', trigger: 'change' }
                    ],
                }
            }
        },
        created() {
            this.initTable();
            this.queryDept();
            //状态
            this.initStatus();
            //药品类型
            this.initStatus1();
            //处方类型
            this.initStatus2();
            //生产厂家
            this.queryDept();
        }
    }
</script>

<style scoped>
    .demo-table-expand {
        font-size: 0;
    }

    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }

    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }

</style>
